local UIBase = require("view.UIBase")
local FightEffectText = class("FightEffectText", UIBase)
local Color = require("utils.Color")
local Vector3 = require("utils.Vector3")
local Vector2 = require("utils.Vector2")
local GameUtils = require("utils.GameUtils")

function FightEffectText:ctor(id, parent)
    self._id = id
    self._actor = parent
    local resource = {
        path = "Prefabs/UI",
        name = "FightEffectText",
        canvas = 1
    }
    FightEffectText.super.ctor(self, resource)
end

function FightEffectText:CalcTextWidth(text)
    local width = text.preferredWidth
    return width
end

function FightEffectText:updatePosition()
    local trans = self._actor.transform
    local screenPoint = self._mainCamera:WorldToScreenPoint(trans.position)
    local screenPoint2 = Vector2.new(screenPoint.x, screenPoint.y)
    local outPos = G_UIManager:ScreenPointToLocalPointInRectangle(self._parentRect, screenPoint2, self._uiCamera)
    self._rect.localPosition = Vector3.new(outPos.x, outPos.y + 120, 0)
end

function FightEffectText:start()
    self._rect = self.gameObject:GetComponent(typeof(CS.UnityEngine.RectTransform))
    self._uiCamera = G_UIManager:getUICamera()
    self._mainCamera = G_UIUtil.getMainCamera()
    self._parentRect = self.transform.parent:GetComponent(typeof(CS.UnityEngine.RectTransform))
    self:updatePosition()

    local cfg = require("config.fight_effect").get(self._id)
    self._textTip.text = cfg.name
    self._textName.text = ""
    if cfg.arrow == 1 then
        local width = self:CalcTextWidth(self._textTip)
        self._imageArrow.rectTransform.localPosition = Vector3.new(-width * 0.5 - 10, 0, 0)
        self._imageArrow.rectTransform.localEulerAngles = Vector3.new(0, 0, 90)
    elseif cfg.arrow == 2 then
        self._textTip.color = Color.RED_COLOR
        self._textName.color = Color.RED_COLOR
        local width = self:CalcTextWidth(self._textTip)
        self._imageArrow.rectTransform.localPosition = Vector3.new(-width * 0.5 - 10, 0, 0)
        self._imageArrow.rectTransform.localEulerAngles = Vector3.new(0, 0, -90)
    elseif cfg.arrow == 3 then
        self._imageArrow.gameObject:SetActive(false)
    end
    local sequence = DOTween.Sequence()
    local posY = self.transform.localPosition.y
    local move = self.transform:DOLocalMoveY(posY + 50, 2)
    local fade1 = self._textName:DOFade(2)
    local fade2 = self._textTip:DOFade(2)
    local fade3 = self._imageArrow:DOFade(2)
    sequence:Append(move)
    sequence:Join(fade1)
    sequence:Join(fade2)
    sequence:Join(fade3)
    sequence:AppendCallback(
        function()
            self:close()
        end
    )
end

return FightEffectText
